<template>
  <div class="template-list">
    <div>
      <Button type="primary" @click="addTemplate()">新增模板</Button>
    </div>
    <div>
      <Table class="template-content" :columns="columns7" :data="templateData">
        <template slot-scope="{ row }" slot="action">
          <div class="table-form-action-box">
            <span class="table-blue action-btn" @click="toEdit(row)">编辑</span>
            <span class="table-blue table-red" @click="toDel(row)">删除</span>
          </div>
        </template>
      </Table>
      <Page
        style="margin-bottom: 10px;"
        v-show="pagination.total > pagination.pageSize || pagination.pageSize > 10"
        @on-change="changePageNum"
        @on-page-size-change="changePageSize"
        :total="pagination.total"
        :current="pagination.current"
        :page-size="pagination.pageSize"
        :transfer="true"
        show-sizer
        show-total
      />
    </div>
  </div>
</template>

<script>
import { getTemplateList, delTemplate } from 'http/api/mark';
import { routePathData } from 'assets/scripts/common'
export default {
  name: 'LineList',
  data () {
    return {
      pagination: {
        total: 10,
        current: 1,
        pageSize: 10
      },
      columns7: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: 'templateName',
          key: 'name',
          width: 300
        },
        {
          title: 'templateId',
          key: 'id'
        },
        {
          title: '操作',
          key: 'action',
          width: 150,
          align: 'center',
          slot: 'action'
        }
      ],
      templateData: []
    };
  },

  methods: {
    addTemplate () {
      this.$router.push({ path: routePathData.markVisual });
    },
    // 模板详情
    toEdit (row) {
      this.$router.push({ path: `${routePathData.markVisual}?id=${row.id}` });
    },
    // 删除模板
    toDel (row) {
      this.$Modal.confirm({
        title: '确认要删除吗',
        content: '<p>数据删除后不可恢复</p>',
        onOk: async () => {
          await delTemplate(row.id);
          if (
            this.pagination.total > this.pagination.pageSize &&
            this.pagination.pageSize * (this.pagination.current - 1) + 1 ===
            this.pagination.total
          ) {
            this.pagination.current--;
          }
          this.getTemplateList();
        }
      });
    },
    changePageNum (pageNum) {
      this.pagination.current = pageNum;
      this.getTemplateList();
    },
    changePageSize (size) {
      this.pagination.pageSize = size;
      this.changePageNum(1);
    },
    getTemplateList () {
      let param = {
        page: this.pagination.current,
        size: this.pagination.pageSize
      };
      getTemplateList(param).then(res => {
        if (res) {
          this.templateData = res.items;
          this.pagination.total = res.total;
        }
      })
        .catch(err => {
          this.$Message.error(err);
        })
    }
  },
  mounted () {
    this.getTemplateList();
  }
};
</script>

<style rel="stylesheet/less" lang="less" scoped>
.template-list {
  padding: 20px;
  .template-content {
    margin: 20px 0;
  }
}
</style>

<style lang="less">
</style>
